package net.ak.service.impl;

import net.ak.response.AKResponse;
import net.ak.service.UserService;
import net.ak.utils.ConfigurationUtils;

import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

@Service
public class UserServiceImpl implements UserService {

	@Override
	public AKResponse login(String u, String p, String lat, String lng) {
		RestTemplate restTemplate = new RestTemplate();
		String restUrl = String.format("%s/authenticate",
				ConfigurationUtils.BASE_USER_API_URL);
		MultiValueMap<String, Object> formData = new LinkedMultiValueMap<String, Object>();
		formData.add("u", u);
		formData.add("p", p);
		formData.add("lat", lat);
		formData.add("lng", lng);
		System.out.println(restUrl);
		return restTemplate.postForObject(restUrl, formData, AKResponse.class);
	}

	@Override
	public AKResponse listAllCategory(String session) {
		RestTemplate restTemplate = new RestTemplate();
		try {
			String restUrl = String.format("%s/getAllCategory?session=%s", ConfigurationUtils.BASE_USER_API_URL, session);
			System.out.println(restUrl);
			return restTemplate.getForObject(restUrl, AKResponse.class);
		} catch (Exception e){}
		return null;
	}

	@Override
	public AKResponse getCategoryById(String session, String catId) {
		RestTemplate restTemplate = new RestTemplate();
		try {
			String restUrl = String.format("%s/selectCategory?session=%s&catId=%s", ConfigurationUtils.BASE_USER_API_URL, session, catId);
			System.out.println(restUrl);
			return restTemplate.getForObject(restUrl, AKResponse.class);
		} catch (Exception e){}
		return null;
	}

	@Override
	public AKResponse registerCategory(String session, String catId, String cost, String desc, String paypalEmail) {
		RestTemplate restTemplate = new RestTemplate();
		String restUrl = String.format("%s/registerCategory",
				ConfigurationUtils.BASE_USER_API_URL);
		MultiValueMap<String, Object> formData = new LinkedMultiValueMap<String, Object>();
		System.out.println("session -> " + session + ": category id -->" + catId + ": cost  ->" + cost + ": desc -> " + desc);
		formData.add("session", session);
		formData.add("catId", catId);
		formData.add("cost", cost);
		formData.add("desc", desc);
		formData.add("paypalEmail", paypalEmail);
		System.out.println(restUrl);
		return restTemplate.postForObject(restUrl, formData, AKResponse.class);
	}



}
